<?php

/**
 * The LayoutPage abstract class is the model of the Design Pattern.
 * 
 * @author Cesar Augustus Silva <cesaraugustussilva@linuxmail.org>
 * @copyright Copyright (c) 2013, Cesar Augustus Silva
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL v3.0
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <­http://www.gnu.org/licenses/>.
 */
abstract class LayoutPage {

    /** @var boolean Defines whether the system has already been initialized */
    protected static $initialized = false;
    
    /** @var array The banners */
    public static $banners = array();
    
    /**
     * The destructor magic method of the LayoutPage class
     * 
     * @return void No value is returned
     */
    public function __destruct() {
        Db::disconnect();
    }
    
    /**
     * Method of execution of the system
     * 
     * @return void No value is returned
     */
    public function run() {
        $this->init();
        $this->preProcess();
        $this->displayHeader();
        $this->process();
        $this->displayContent();
        $this->displayFooter();
    }
    
    /**
     * Method initial system boot
     * 
     * @global array $css_files The list of styles
     * @global array $js_files The list of scripts
     * @return void No value is returned
     */
    public function init() {
        global $css_files, $js_files;
        
        if (!self::$initialized) {
            self::$initialized = true;

            $css_files = array();
            $js_files = array();

            ob_start();

            $this->setMedia();
        }
    }
    
    /**
     * Method used to add links of styles and scripts
     * 
     * @return void No value is returned
     */
    public function setMedia() {
        Shell::addStyle(array('reset','style'));
        Shell::addScript(array('placeholder','jquery.min','script'));
    }
    
    /**
     * Method used to add process before displaying the header
     * 
     * @return void No value is returned
     */
    public function preProcess() {
        if (!self::$initialized)
            $this->init();
        
        $config = new Session('config');
        $config->iframe = (bool) $_GET['iframe'];
    }
    
    /**
     * Method used to start the output of an HTML header
     * 
     * @return void No value is returned
     */
    public function displayHeader() {
        if (!self::$initialized)
            $this->init();
        
        self::$banners['top'] = Util::view_banners('top');
        Shell::showPage('head');
        Shell::showPage('header');
    }
    
    /**
     * Method used to add process after displaying the header
     * 
     * @return void No value is returned
     */
    public function process() {
        if (!self::$initialized)
            $this->init();
        
        self::$banners['tv'] = Util::view_banners('tv');
        self::$banners['lateral3'] = Util::view_banners('lateral3');
    }
    
    /**
     * Methods used to display the contents
     * 
     * @return void No value is returned
     */
    public function displayContent() {
        if (!self::$initialized)
            $this->init();
        
        Shell::addWidget(array('search-detailed','banners-tv','banners-lateral'), 'wrapper-header');
    }
    
    /**
     * Method used to display the footer
     * 
     * @return void No value is returned
     */
    public function displayFooter() {
        if (!self::$initialized)
            $this->init();
            
        Shell::showPage('footer');
    }

}